<?php
namespace Api\Model;
use Think\Model;

class AreaModel extends Model{
	//ID转地区名称
	public function id2names($area_ids){
		$area_ids = explode(',', $area_ids);
		$name = '';
		if($area_ids){
			$where['id'] = array('in',$area_ids);
			$name = $this->where($where)->cache(true)->getField('name',true);
			$name = implode(',', $name);
		}
		return $name?$name:'';
	}
	/**
	 * 获取区域列表
	 * @param  string $type 级别：0国家，1省份，2城市，3地区
	 */
	public function get_list($type=''){
		if($type){
			$where['type'] = $type;
		}
		$where['is_open'] = 1;
		$list = D('Area')->field('id city_id,name,parentid,is_open')
						 ->order('order_id,id')
						 ->cache(true)
						 ->where($where)
						 ->select();
		return $list;
	}
	public function get_id_name_list($type=''){
		$list = $this->get_list($type);
		foreach ($list as $rs) {
			$area_id_name[$rs['city_id']] = $rs['name'];
		}
		return $area_id_name;
	}
	/**
	 * 获取上级信息
	 * @param  [type] $parentid [description]
	 * @return [type]           [description]
	 */
	public function get_parent_info($parentid){
		if(!$parentid){
			return (new stdClass);
		}
		$where['id'] = $parentid;
		$data = D('Area')->field('id,parentid,name,is_open')->where($where)->find();
		if($data){
			import("ORG.Util.Pinyin");
			$py = new PinYin();
			$letter = $py->getAllPY($data['name']);
			$data['city_id']       = $data['id'];
			$data['letter_all']    = $letter;
			$data['letter_simple'] = strtoupper($py->getFirstPY($data['name']));
			$data['parent_name']   = D('Area')->id2names($data['parentid']);
			$data['letter_first']  = substr($letter, 0,1);
		}else{
			$data = (new stdClass);
		}
		return $data;
	}
	/**
	 * 获取热门城市
	 * @return [type] [description]
	 */
	public function get_hot_city(){
		$area_id_name = $this->get_id_name_list(1);
		//热门城市
		$where['type']   = 2;
		$where['is_hot'] = 1;
		$list = D('Area')->field('id city_id,name,parentid,is_open')->where($where)->order('order_id,id')->select();
		$data = array();
		import("ORG.Util.Pinyin");
		$py = new PinYin();
		foreach ($list as $rs) {
			$letter = $py->getAllPY($rs['name']);
			$letter = strtoupper($letter);
			$rs['letter_all']    = $letter;
			$rs['letter_simple'] = strtoupper($py->getFirstPY($rs['name']));
			$rs['parent_name']   = D('Area')->id2names($rs['parentid']);
			$rs['is_open']       = $rs['is_open'] == 1;

			$data[] = $rs;
		}
		return $data;
	}
	/**
	 * 获取开通城市
	 * @return [type] [description]
	 */
	public function get_open_city($limit_num=''){
		$area_id_name = $this->get_id_name_list(1);
		
		//热门城市
		$where['type']    = 2;
		$where['is_open'] = 1;
		$list = D('Area')->field('id city_id,name,parentid')->where($where)->order('order_id,id')->limit($limit_num)->select();
		$data = array();
		foreach ($list as $rs) {
			$parent_name = $area_id_name[$rs['parentid']];
			$rs['parent_name'] = $parent_name?$parent_name:'';
			//APP，去掉厦门
			// $app_type = $_SERVER['HTTP_APP_TYPE'];
			// $app_type = $app_type ? $app_type : 1;
			// if(in_array($app_type, array(2)) && $rs['city_id'] == '60'){
			// 	continue;
			// }
			$data[] = $rs;
		}
		return $data;
	}
	/**
	 * 获取老师授课区域
	 * @param  [type] $user_id [description]
	 * @return [type]          [description]
	 */
	public function get_teaching_area_name($user_id,$is_all=true){
		$where['user_id'] = $user_id;
		$list = D('TeachingArea')->where($where)->select();
		foreach ($list as $rs) {
			$area_ids[] = $rs['area_id'];
		}
		if($is_all == true){
			$area_ids = implode(',', $area_ids);
			$area_name = $this->id2names($area_ids);
		}else{
			$area_id    = $area_ids[0];
			$area_name   = $this->id2names($area_id);
			$parent_name = $this->get_parent_name($area_id);
			$parent_name = $parent_name ? $parent_name . '-' : '';
			$area_name   = $parent_name . $area_name;
		}
		return $area_name ? $area_name : '';
	}
	/**
	 * 获取父ID
	 * @param  [type] $area_id 地区ID
	 * @return [type]          [description]
	 */
	public function get_parent_id($area_id){
		$where['id'] = $area_id;
		$parent_id = D('Area')->where($where)->getField('parentid');
		return $parent_id?$parent_id:'';
	}
	/**
	 * 获取上级名称
	 * @param  [type] $area_id 地区ID
	 */
	public function get_parent_name($area_id){
		$where['id'] = $this->get_parent_id($area_id);
		$name = D('Area')->where($where)->getField('name');
		return $name?$name:'';
	}
	/**
	 * 获取下级ID
	 * @param  [type] $parentid 父ID
	 */
	public function get_lower_level_id($parentid){
		$where['parentid'] = $parentid;
		$level_id = D('Area')->where($where)->getField('id',true);
		return $level_id?$level_id:'';
	}
	public function get_city_area_name($user_id,$area_id){
		if(!$area_id){
			$where['user_id'] = $user_id;
			$area_id = D('TeachingArea')->where($where)->getField('area_id');
		}
		$area_name = $this->id2names($area_id);
		$city_id   = $this->get_parent_id($area_id);
		$city_name = $this->id2names($city_id);

		return $city_name.$area_name;
	}
}
?>